package com.kxmall.web.controller.rider.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kxmall.common.core.domain.PageQuery;
import com.kxmall.common.core.page.TableDataInfo;
import com.kxmall.rider.domain.KxRiderCycle;
import com.kxmall.rider.domain.bo.KxRiderCycleBo;
import com.kxmall.rider.domain.vo.KxRiderCycleVo;
import com.kxmall.rider.mapper.KxRiderCycleMapper;
import com.kxmall.web.controller.rider.service.IKxRiderCycleService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 配送员周期Service业务层处理
 *
 * @author kxmall
 * @date 2023-09-21
 */
@RequiredArgsConstructor
@Service
public class KxRiderCycleServiceImpl implements IKxRiderCycleService {

    private final KxRiderCycleMapper baseMapper;

    /**
     * 查询配送员周期
     */
    @Override
    public KxRiderCycleVo queryById(Long id) {
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询配送员周期列表
     */
    @Override
    public TableDataInfo<KxRiderCycleVo> queryPageList(KxRiderCycleBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<KxRiderCycle> lqw = buildQueryWrapper(bo);
        Page<KxRiderCycleVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询配送员周期列表
     */
    @Override
    public List<KxRiderCycleVo> queryList(KxRiderCycleBo bo) {
        LambdaQueryWrapper<KxRiderCycle> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<KxRiderCycle> buildQueryWrapper(KxRiderCycleBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<KxRiderCycle> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getRiderId() != null, KxRiderCycle::getRiderId, bo.getRiderId());
        lqw.eq(bo.getWeekNumber() != null, KxRiderCycle::getWeekNumber, bo.getWeekNumber());
        return lqw;
    }

    /**
     * 新增配送员周期
     */
    @Override
    public Boolean insertByBo(KxRiderCycleBo bo) {
        KxRiderCycle add = BeanUtil.toBean(bo, KxRiderCycle.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改配送员周期
     */
    @Override
    public Boolean updateByBo(KxRiderCycleBo bo) {
        KxRiderCycle update = BeanUtil.toBean(bo, KxRiderCycle.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(KxRiderCycle entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除配送员周期
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
